﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 易发车网.Untils;
using 易发车网.WebResult;

namespace 易发车网
{
    public static class AppData
    {
        public static UserSet UserSet = new UserSet();

        public static List<SellInfo> SellRecods = new List<SellInfo>();

        /// <summary>
        /// 读取配置信息
        /// </summary>
        public static void ReadUserSet()
        {
            if (File.Exists("userset.json"))
            {
                try
                {
                    UserSet = JsonConvert.DeserializeObject<UserSet>(File.ReadAllText("userset.json"));
                }
                catch
                {

                }
            }
        }

        /// <summary>
        /// 保存配置信息
        /// </summary>
        /// <returns></returns>
        public static bool SaveUserSet()
        {
            try
            {
                File.WriteAllText("userset.json", JsonConvert.SerializeObject(UserSet));
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 读取记录
        /// </summary>
        public static void ReadSellRecords()
        {
            return;
            if (File.Exists("data.json"))
            {
                try
                {
                    SellRecods = JsonConvert.DeserializeObject<List<SellInfo>>(File.ReadAllText("data.json"));
                }
                catch
                {

                }
            }
        }

        /// <summary>
        /// 保存记录到文件
        /// </summary>
        public static bool SaveSellRecords()
        {
            return true ;
            try
            {
                File.WriteAllText("data.json", JsonConvert.SerializeObject(SellRecods));
                return true;
            }
            catch
            {
                return false;
            }

        }

        /// <summary>
        /// 添加并检测是否存在
        /// </summary>
        public static void AddRecord(SellInfo info)
        {
            if (!IsRecordExists(info))
            {
                SellRecods.Add(info);
            }
        }

        /// <summary>
        /// 批量添加记录
        /// </summary>
        /// <returns></returns>
        public static void AddRecords(List<SellInfo> infos)
        {
            foreach (var info in infos)
            {
                AddRecord(info);
            }
        }

        /// <summary>
        /// 是否存在了这个记录
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public static bool IsRecordExists(SellInfo info)
        {
            return SellRecods.Exists(s => s.Vid == info.Vid);
        }
    }
}
